package com.wl.api.controller.system;

import com.wl.api.common.annotation.RequiresAuthentication;
import com.wl.api.common.utils.*;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.base.CommonUserDTO;
import com.wl.api.dto.business.CustomerUserDTO;
import com.wl.api.dto.business.CustomerUserGradeDTO;
import com.wl.api.model.business.CustomerUser;
import com.wl.api.service.business.ICustomerUserService;
import com.wl.api.service.system.ISecurityChannelAreaLadderPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 
 * @author cj member manage
 *  customer manage
 */
@RestController
@RequestMapping("/admin/customer/manage")
@RequiresAuthentication(getAuthticationSn = "customer")
public class AdminCustomerController extends CommonController {
	
	@Autowired
	ICustomerUserService customerUserService;

	@Autowired
	ISecurityChannelAreaLadderPriceService areaLadderPriceService;

	@RequestMapping(value = "/bathUpdateGrade", method = RequestMethod.PUT)
	public JsonObject batchUpdateCustomerGrade(CustomerUserGradeDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		String title = "", code = "";
		boolean pass = true;
		if (param.getGradeId() == null || CollectionUtil.isEmpty(param.getUserIds())) {
			pass = false;
			title = ErrorCodeEnum.param_error.getMessage();
			code = ErrorCodeEnum.param_error.getError_code();
		}
		if (pass) {
			CommonUserDTO user = getCommonUser();
			param.buildUpdateMsg(user.getId(), user.getUsername());
			customerUserService.batchUpdateCustomerByIds(param);
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}
	
	/**
	 *  update 
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/member", method = RequestMethod.PUT)
	public JsonObject modify(@RequestBody CustomerUserDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "" , error_code = ""; 
		if (ObjectUtil.isEmpty(param.getCustId())) {
			title = ErrorCodeEnum.param_error.getMessage();
			error_code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		if(pass) {
			CommonUserDTO user = getCommonUser();
			param.setPassword(null);
			param.buildUpdateMsg(user.getId(), user.getUsername());
			customerUserService.updateById(BeanUtils.copyProperties(param, CustomerUser.class));
		}
		setJsonMessage(json, title, error_code, pass);
		return json;
	}
	
	@RequestMapping(value="/member", method = RequestMethod.GET)
	public JsonObject getItemList(CustomerUserDTO param) {
		JsonObject json = JsonObject.getInstance();
		param.buildStartTime(param.getStartTime()).buildEndTime(param.getEndTime()).buildPage(param.getPage(), param.getPageSize());
		PageInfo<CustomerUserDTO> pageInfo =  customerUserService.getCustomerUserPage(param);
		json.setPage(pageInfo);
		setJsonMessage(json);
		return json;
	}
	@RequestMapping(value="/member/{custId}", method = RequestMethod.DELETE)
	public JsonObject delCustomerUser(@PathVariable("custId") Long custId) throws Exception {
		JsonObject json = JsonObject.getInstance();
		CommonUserDTO user = getCommonUser();
		customerUserService.updateById(CustomerUser.builder().isDeleted(1).updateTime(new Date()).build());
		return json;
	}

	
	private void initParam(CustomerUserDTO param) {
			if(StringUtil.isEmpty(param.getAddress())) {
				param.setAddress(null);
			}
			if (StringUtil.isEmpty(param.getUsername())) {
				param.setUsername(null);
			}
	}
	
}